package cn.fengshenghospital.service.impl;

import cn.fengshenghospital.entity.Article;
import cn.fengshenghospital.entity.Logs;
import cn.fengshenghospital.mapper.ArticleMapper;
import cn.fengshenghospital.mapper.LogsMapper;
import cn.fengshenghospital.service.ArticleService;
import cn.fengshenghospital.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private LogsMapper logsMapper;
    //根据菜单查询文章内容
    @Override
    public List<Article> getArticleByMid(int mid,int pageIndex,int endSize) throws Exception {
        return articleMapper.getArticleByMid(mid,(pageIndex-1)*endSize,endSize);
    }

    //查询总页数
    @Override
    public int getArticleCount(int mid)throws Exception {
        return articleMapper.getArticleCount(mid);
    }

    //根据条件搜索文章
    @Override
    public List<Article> searchArticle(Map map)throws Exception  {
        return articleMapper.searchArticle(map);
    }

    //根据条件搜索文章总页数
    @Cacheable(cacheNames = "article",keyGenerator = "myKeyGenerator",unless = "#result==0")
    @Override
    public int searchArticleCount(Map map) {
        return articleMapper.searchArticleCount(map);
    }

    //根据文章编号搜索文章内容
    @Override
    public Article getArticleById(int id) throws Exception {
        return articleMapper.getArticleById(id);
    }

    @Override
    public int addArticleById(Article article, Logs logs) throws Exception {
        int a = logsMapper.addLogs(logs);
        int i =  articleMapper.addArticleById(article);
        return i;
    }

    @Override
    public int updateArticle(Article article, Logs logs) throws Exception {
        int a = logsMapper.addLogs(logs);
        int i =  articleMapper.updateArticle(article);
        return i;
    }

    @Override
    public List<Article> queryArticle(Map map) {
        int beginPage = 0;
        if(map.get("page") != null) {
            beginPage = ((Integer) map.get("page") - 1) * Constants.PageSize;
            map.put("beginPage",beginPage);
        }
        return articleMapper.queryArticle(map);
    }

    @Override
    public int queryArticleCount(Map map) {
        int totalCount = articleMapper.queryArticleCount(map);
        int totalSize = totalCount% Constants.PageSize==0?totalCount/Constants.PageSize:totalCount/Constants.PageSize+1;
        return totalSize;
    }


}
